<?php


namespace App\Services\Api;


use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class UploadService
{

    /**
     * 图片上传修改
     * @param Request $request
     * @return string
     */
    public function setPicture(Request $request)
    {
        $file = $request->file('picture'); // image base64 encoded
        $result = [
            'status' => 0,
            'msg' => '上传失败!',
            'data' => []
        ];
        //验证文件
        if($file->isValid()){
            $extension = $file->getClientOriginalExtension(); //上传文件的后缀.
            $filename = $file->getClientOriginalName();

            //验证后缀
            $allow_ext = ['jpg', 'png', 'jpeg', 'gif'];
            if(!in_array(strtolower($extension), $allow_ext)){
                $result['msg'] = '文件格式不正确!';
                return response()->json($result);
            }

            $imageName = 'photo'; //generating unique file name;
            $disk      = Storage::disk('oss');
            $path      = $disk->put($imageName,$file);
            return $disk->url($path);

        }

        //返回响应
        return response()->json($result);
    }
}
